package cn.gzsxt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import cn.gzsxt.dao.TaskCheckDao;
import cn.gzsxt.po.T_sysUser;
import cn.gzsxt.po.T_task;
import cn.gzsxt.po.Uname;
import cn.gzsxt.query.PageQueryObject;
import cn.gzsxt.utils.DataUtil;

public class TaskCheckDaoImpl implements TaskCheckDao {

	@Override
	public Integer checkTotalCountByCondition(PageQueryObject qo) {
		String sql = "select count(*) from t_task";
		if (qo.getKeyword() != null && !"".equals(qo.getKeyword().trim())) {
			sql = "select count(*) from t_task where title like '%" + qo.getKeyword() + "%'";
		}
		return DataUtil.select(sql);
	}

	@Override
	public List<T_task> checkTask(PageQueryObject qo) {
		String sql = "select * from t_task limit ?,?";
		if (qo.getKeyword() != null && !"".equals(qo.getKeyword().trim())) {
			sql = "select * from t_task where title like '%' ? '%' limit ?,?";
			return DataUtil.selectData(sql, T_task.class, qo.getKeyword(), (qo.getCurrentPage() - 1) * qo.getPageSize(),
					qo.getPageSize());
		}
		return DataUtil.selectData(sql, T_task.class, (qo.getCurrentPage() - 1) * qo.getPageSize(), qo.getPageSize());
	}

	@Override
	public List<Uname> getName(Object[] temp) {
		List<Uname> list = new ArrayList<>();
		for (int i = 0; i < temp.length; i++) {
			String sql = "select uname from t_sysuser where id = " + temp[i];
			String value = DataUtil.selectName(sql);
			Uname name = new Uname();
			name.setKey("" + temp[i]);
			name.setValue(value);
			list.add(name);
		}

		return list;
	}

	@Override
	public Integer delTaskById(String id) {
		T_task temp = new T_task();
		temp.setId(Integer.valueOf(id));
		return DataUtil.deleteData(temp);
	}

	@Override
	public int updateTaskById(T_task task, Integer... id) {
		int row = 0;
		if (id.length == 0) {
			row = DataUtil.insertData(task);
		} else {
			Integer key = id[0];
			row = DataUtil.insertData(task, id);
		}
		task.setId(null);
		return row;
	}

	@Override
	public List<T_sysUser> checkUserINameId() {
		String sql = "select id,uname from t_sysuser";
		return DataUtil.selectData(sql, T_sysUser.class);
	}

	@Override
	public List<T_task> checkTaskById(String id) {
		String sql = "select * from t_task where id = ?";
		return DataUtil.selectData(sql, T_task.class, id);
	}

	@Override
	public Integer delPageTaskById(String[] ids) {
		String sql = "delete from t_task  where id = ?";
		return DataUtil.transaction(sql, ids);
	}

}
